<template>
	<div class='system-notice-container'>
		<vxe-grid v-bind='gridConfig'>
			<template v-slot:toolbar_tools>
				<n-flex>
					<n-button circle strong @click='initTableData'>
						<i class='fa fa-refresh'/>
					</n-button>
					<n-button circle strong @click='more'>
						<i class='fa fa-list'/>
					</n-button>
				</n-flex>
			</template>
		</vxe-grid>
	</div>
</template>
<script lang='ts' setup>
import { onMounted, reactive, ref } from 'vue';
import axios from '@/config/axios/index.ts';
import { VxeUI, VxeGrid } from '@/config/vxe/index.ts';
import { useTabsStore } from '@/config/pinia/tabs.store.ts';
import { useMenusStore } from '@/config/pinia/menus.store.ts';
import type { JsonResultProps } from '#/JsonResult.d.ts';
import type { CommonMenuProps } from '#/Menu.d.ts';
import { noticeDetailPath } from '@/config/pinia/menus.store.config.ts';
import { VxeTableDefines, VxeTablePropTypes } from 'vxe-table';
import type { SystemNoticeInfoProps } from '#/src/components/page/systemManager/noticeMaintain/SystemNoticeProps.d.ts';
import { sysNoticePath } from '@/config/config.ts';
const tabsStore = useTabsStore();
const menusStore = useMenusStore();
// @ts-ignore
const gridConfig = reactive<VxeGridProps>({
	loading        : true,
	keyField       : 'id',
	height         : 'auto',
	align          : 'center',
	showHeader     : false,
	keyboardConfig : { isArrow : true },
	pagerConfig    : {
		enabled : false
	},
	toolbarConfig  : {
		slots : {
			tools : 'toolbar_tools'
		}
	},
	formConfig     : {
		enabled : false,
		data    : {}
	},
	columns        : [
		{
			field        : 'noticeCode',
			width        : '30%',
			showOverflow : 'tooltip',
			formatter    : (params: {
				cellValue: any,
				column: VxeTableDefines.ColumnInfo<VxeTablePropTypes.Row>,
				row: VxeTablePropTypes.Row
			}) => {
				return params.row.title;
			}
		},
		{
			width        : '50%',
			showOverflow : 'tooltip',
			formatter    : (params: {
				cellValue: any,
				column: VxeTableDefines.ColumnInfo<VxeTablePropTypes.Row>,
				row: VxeTablePropTypes.Row
			}) => {
				return params.row.title;
			}
		},
		{
			width     : '20%',
			formatter : (params: {
				cellValue: any,
				column: VxeTableDefines.ColumnInfo<VxeTablePropTypes.Row>,
				row: VxeTablePropTypes.Row
			}) => {
				if (params.row.isLongTime) {
					return '长期';
				}
				return params.row.startTime;
			}
		}
	],
	data           : [],
	// @ts-ignore
	onCellClick : (params: any) => goToDetail(params)
});
onMounted(() => {
	initTableData();
});
const initTableData = () => {
	gridConfig['loading'] = true;
	axios<any, JsonResultProps<any>, any>({
		method : 'GET',
		url    : `/notice/list/${sessionStorage.getItem('loginTenantCode')}`
	}).then((jsonResult: JsonResultProps<any>) => {
		if (jsonResult.status) {
			gridConfig.data = jsonResult.data;
		} else {
			VxeUI.modal.message({
				content : jsonResult['msg'],
				status  : 'warning',
				id      : 'failedMessage'
			});
		}
		gridConfig['loading'] = false;
	}).catch((errorObj: unknown) => {
		gridConfig['loading'] = false;
		console.error(errorObj);
		VxeUI.modal.message({ content : '系统异常,请稍后再试', status : 'error', id : 'errorMessage' });
	});
};
const goToDetail = ({ row }: { row: SystemNoticeInfoProps }) => {
	const noticeDetail: CommonMenuProps = {
		id           : `systemNoticeDetail_${row.noticeCode}`,
		key          : `systemNoticeDetail_${row.noticeCode}`,
		menuCode     : `systemNoticeDetail_${row.noticeCode}`,
		menuName     : '公告明细',
		menuType     : 'MENU',
		menuPath     : noticeDetailPath,
		menuIcon     : null,
		menuIconType : null,
		tabIcon      : 'fa fa-bell',
		tabIconType  : 'class',
		isCache      : true,
		// forceFlush   : false,
		params : {
			noticeCode : row.noticeCode
		}
	};
	tabsStore.addTab(noticeDetail);
};
const more = async () => {
	recursionFindNoticeMaintain(menusStore.menuList);
	const menu = noticeMaintainMenu.value;
	if (menu) {
		await tabsStore.addTab({
			id           : menu.id,
			key          : menu.menuCode,
			menuCode     : menu.menuCode,
			menuName     : menu.menuName,
			menuType     : menu.menuType,
			menuPath     : menu.menuPath,
			menuIcon     : menu.menuIcon,
			menuIconType : menu.menuIconType,
			tabIcon      : menu.tabIcon,
			tabIconType  : menu.tabIconType,
			isCache      : menu.isCache,
			// forceFlush   : false
			params : null
		});
	}
	// const menu = noticeMaintainMenu.value;
};
const noticeMaintainMenu = ref();
const recursionFindNoticeMaintain = (menuList: Array<CommonMenuProps>) => {
	// 'page/systemManager/noticeMaintain/index' === item.menuPath
	const flag = menuList && Array.isArray(menuList) && menuList.length;
	if (flag) {
		menuList.forEach((item: CommonMenuProps) => {
			if (item.children && Array.isArray(item.children) && item.children.length) {
				recursionFindNoticeMaintain(item.children);
			} else {
				if (sysNoticePath === item.menuPath) {
					noticeMaintainMenu.value = item;
					return;
				}
			}
		});
	}
};
</script>
<style lang='scss' scoped>
.system-notice-container{
	box-sizing :border-box;
	margin     :0;
	padding    :0;
	border     :0;
	// border-radius :8px;
	height     :100%;
	width      :100%;
}
</style>